package face.pay.api.payter.channel.paytype;

import face.pay.api.payter.channel.paytype.message.MessageSender;
import face.pay.dao.CpnCardDetailInfoDao;
import face.pay.entity.CpnCardDetailInfo;
import face.pay.entity.OrderInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@Slf4j
public abstract class AbstractApi {

    @Autowired
    public MessageSender messageSender;

    @Autowired
    public CpnCardDetailInfoDao cpnCardDetailInfoDao;


    @Transactional
    public void sendAllMessage(OrderInfo orderInfo, String type){

        try{

            /**
             * 分佣消息
             */
            log.info("发送分佣消息");
            this.messageSender.sendMessage(orderInfo, type);

            Long cardDetailId = orderInfo.getCardDetailId();

            if(cardDetailId!=null && cardDetailId>0){
                log.info("修改优惠券使用状态");

                CpnCardDetailInfo cpnCardDetailInfo = this.cpnCardDetailInfoDao.getById(cardDetailId);

                if(cpnCardDetailInfo!=null){
                    cpnCardDetailInfo.setStatus(3);
                    this.cpnCardDetailInfoDao.update(cpnCardDetailInfo);
                }
                log.info("发送分账消息");
                this.messageSender.sendSharingMessage(orderInfo, type);
            }

        }catch (Exception e){
            log.error("发送消息失败",e);
        }


    };
}
